package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ciei.dpagm.common.enums.ErrorCodeEnum;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.*;
import com.ciei.dpagm.mapper.ChipPinParameterMapper;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangyan
 * @date 2023/05/08
 */
@Service
public class ChipPinParameterService extends BaseService<ChipPinParameterMapper, ChipPinParameter> {

    @Autowired
    private ChipPinService chipPinService;

    @Autowired
    private ChipInterfaceService chipInterfaceService;

    @Autowired
    private ChipInterfacePinFunctionService chipInterfacePinFunctionService;

    @Autowired
    private ChipInterfacePinFunctionParameterService chipInterfacePinFunctionParameterService;


    /**
     * 获取列表
     * @param webData
     * @return
     */
    public JSONObject getList(JSONObject webData){
        String pinName = webData.getString("pinName");
        String resourceName = webData.getString("resourceName");
        String pinCode = webData.getString("pinCode");
        Integer masterChipId = webData.getInteger("masterChipId");
        Map<SFunction<ChipPin, ?>, Object> chipPinFieldValueMap = new HashMap<>();
        chipPinFieldValueMap.put(ChipPin::getPinCode,pinCode);
        chipPinFieldValueMap.put(ChipPin::getChipId,masterChipId);
        List<ChipPin> chipPinList = chipPinService.findByProperties(chipPinFieldValueMap);
        if(chipPinList.isEmpty()){
            return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,"根据芯片id和引脚编号找不到对应的引脚数据，请检查");
        }
        if(chipPinList.size() > 1){
            return JsonUtil.getFail(ErrorCodeEnum.DUPLICATE_DATA,"根据芯片id和引脚编号找到多条引脚数据，请检查");
        }
        Map<SFunction<ChipInterface, ?>, Object> chipInterfaceFieldValueMap = new HashMap<>();
        chipInterfaceFieldValueMap.put(ChipInterface::getResourceName,resourceName);
        chipInterfaceFieldValueMap.put(ChipInterface::getChipId,masterChipId);
        List<ChipInterface> chipInterfaceList = chipInterfaceService.findByProperties(chipInterfaceFieldValueMap);
        List<Integer> chipInterfaceIdList = chipInterfaceList.stream().map(ChipInterface::getInterfaceId).collect(Collectors.toList());
        List<ChipInterfacePinFunction> chipInterfacePinFunctionList = chipInterfacePinFunctionService.getList(pinName,chipInterfaceIdList);
        if(chipInterfacePinFunctionList.isEmpty()){
            return JsonUtil.getFail(ErrorCodeEnum.DATA_NOT_FOUND,"根据芯片id、资源名称、引脚名称找不到对应的引脚功能数据，请检查");
        }
        if(chipInterfacePinFunctionList.size() > 1){
            return JsonUtil.getFail(ErrorCodeEnum.DUPLICATE_DATA,"根据芯片id、资源名称、引脚名称找到多条引脚功能数据，请检查");
        }
        ChipPin chipPin = chipPinList.get(0);
        ChipInterfacePinFunction chipInterfacePinFunction = chipInterfacePinFunctionList.get(0);
        List<ChipPinParameter> chipPinParameterList = findByProperty(ChipPinParameter::getPinId, chipPin.getPinId());
        List<ChipInterfacePinFunctionParameter> chipInterfacePinFunctionParameterList = chipInterfacePinFunctionParameterService.findByProperty(ChipInterfacePinFunctionParameter::getInterfacePinFunctionId, chipInterfacePinFunction.getInterfacePinFunctionId());
        List<JSONObject> dataList = new ArrayList<>();
        for(ChipPinParameter chipPinParameter : chipPinParameterList){
            JSONObject data = new JSONObject();
            ChipInterfacePinFunctionParameter chipInterfacePinFunctionParameter = chipInterfacePinFunctionParameterList.stream().filter(c -> chipPinParameter.getSymbol() != null && chipPinParameter.getSymbol().equals(c.getSymbol())).findFirst().orElse(null);
            data.put("masterChipInterfacePinParameterId",chipPinParameter.getPinParameterId());
            data.put("parameterName",chipPinParameter.getParameterName());
            data.put("dataType",chipPinParameter.getDataType());
            data.put("validValues",chipPinParameter.getValidValues());
            data.put("symbol",chipPinParameter.getSymbol());
            data.put("dependencies",chipPinParameter.getDependencies());
            data.put("defaultValue",chipInterfacePinFunctionParameter == null ? "" : chipInterfacePinFunctionParameter.getValue());
            dataList.add(data);
        }
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,dataList);
    }


}
